<template>
    <el-dialog
        title="选择查询控件类型"
        :visible.sync="visible"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :show-close="false"
        width="30%">
        <el-form :model="form" label-position="left" label-width="100px"  size="small" ref="ctrlForm">
            <el-form-item label="控件类型" prop="form.type">
                <el-select v-model="form.type" placeholder="请选择">
                    <el-option
                        v-for="item in ctrlList"
                        :key="item.name"
                        :label="item.name"
                        :value="item.name">
                    </el-option>
                </el-select>
            </el-form-item>
            <el-form-item label="placeholder" prop="form.placeholder">
                <el-input  v-model="form.placeholder" placeholder="提示语"></el-input>
            </el-form-item>
            <template v-if="form.type === 'Select'">
                <template  v-for="(item,index) in form.options" >
                    <el-form-item label="option" :prop="'form.'+index+'.option.name'" >
                        <el-input  v-model="item.name" style="width: 120px" placeholder="name"></el-input>
                        <el-input  v-model="item.value" style="width: 120px" placeholder="value"></el-input>
                        <el-button @click.prevent="removeOption(index)" icon="el-icon-close" circle size="mini"></el-button>
                    </el-form-item>
                </template>
                <el-button @click.prevent="addOption" size="small">添加option</el-button>
            </template>
        </el-form>
        <span slot="footer" class="dialog-footer">
    <el-button @click="cancel">取 消</el-button>
    <el-button type="primary" @click="save">确 定</el-button>
  </span>
    </el-dialog>
</template>

<script>
    export default {
        name: "FilterCtrlType",
        data() {
            return {
                form:{
                    type:'',
                    placeholder:'',
                    options:[]
                },
                ctrlList:[
                    {name:'Input'},
                    {name:'Select'},
                    {name:'DatePicker'},
                ]
            }
        },
        props:{
            visible:false,
            dataValue:undefined
        },
        watch:{
          visible(v){
              console.log('visible--------------------------------',v)
              if(v && this.dataValue && this.dataValue.type){
                  this.form = Object.assign(this.form,{...this.dataValue})
              }
          }
        },
        methods:{
            reset(){
                this.form = {
                    type:'',
                    placeholder:'',
                    options:[]
                }
            },
            cancel(){
                this.reset()
                this.$emit('cancel')
            },
            save(){
                const params = Object.assign({},this.form)
                if(params.type !== 'Select'){
                    delete params.options
                }
                this.reset()
                this.$emit('save',params)
            },
            removeOption(index){
                this.form.options.splice(index,1)
            },
            addOption(){
                this.form.options.push({name:'',value:''})
            }
        }
    }
</script>

<style scoped>

</style>
